/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.client.widget;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;

import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.data.client.loader.RpcProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.RefreshEvent;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.RowExpander;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;
import com.srv.client.Oneortwo;
import com.srv.client.Registry;
import com.srv.shared.ServiceAsync;
import com.srv.shared.SetInfo;
import com.srv.shared.SetInfoProperties;

public class SetInfoListContainer extends VerticalLayoutContainer{

	
	public SetInfoListContainer() {

		final ServiceAsync service = Registry.get(Oneortwo.SERVICE);

		RpcProxy<PagingLoadConfig, PagingLoadResult<SetInfo>> proxy = new RpcProxy<PagingLoadConfig, PagingLoadResult<SetInfo>>() {
			@Override
			public void load(PagingLoadConfig loadConfig, AsyncCallback<PagingLoadResult<SetInfo>> callback) {
				service.getSetInfoList(loadConfig, callback);
			}
		};

		SetInfoProperties props = GWT.create(SetInfoProperties.class);
		
		
		ListStore<SetInfo> store = new ListStore<SetInfo>(new ModelKeyProvider<SetInfo>() {
			@Override
			public String getKey(SetInfo item) {
				return "" + item.getId();
			}
		});		

		final PagingLoader<PagingLoadConfig, PagingLoadResult<SetInfo>> loader = 
				new PagingLoader<PagingLoadConfig, PagingLoadResult<SetInfo>>(proxy);
		loader.setRemoteSort(true);
		loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, SetInfo, PagingLoadResult<SetInfo>>(store));

		final PagingToolBar toolBar = new PagingToolBar(20);
		toolBar.getElement().getStyle().setProperty("borderBottom", "none");
		toolBar.bind(loader);

		IdentityValueProvider<SetInfo> identity = new IdentityValueProvider<SetInfo>();
	
		final CheckBoxSelectionModel<SetInfo> sm = new CheckBoxSelectionModel<SetInfo>(identity) {
			@Override
			protected void onRefresh(RefreshEvent event) {
				// this code selects all rows when paging if the header checkbox is selected
				if (isSelectAllChecked()) {
					selectAll();
				}
				super.onRefresh(event);
			}
		};
		
		RowExpander<SetInfo> expander = new RowExpander<SetInfo>(identity, new AbstractCell<SetInfo>() {
			@Override
			public void render(Context context, SetInfo value, SafeHtmlBuilder sb) {
				/*
				 * Always do a null check on the value. Cell widgets can pass null to
				 * cells if the underlying data contains a null, or if the data arrives
				 * out of order.
				 */				
				if (value == null)
					return;
				sb.appendHtmlConstant("<p style='margin: 5px 5px 10px'>" + value.getFirstCardHtml() + "</p>");
				}
			});
		
	    ColumnConfig<SetInfo, String> keywordColumn = new ColumnConfig<SetInfo, String>(props.keyword(), 150, "Keyword");
	    ColumnConfig<SetInfo, String> authorNameColumn = new ColumnConfig<SetInfo, String>(props.authorName(), 150, "Author");
	    ColumnConfig<SetInfo, String> tagsColumn = new ColumnConfig<SetInfo, String>(props.tags(), 150, "Tags");
	    ColumnConfig<SetInfo, Date> dateColumn = new ColumnConfig<SetInfo, Date>(props.date(), 150, "Date");
	    dateColumn.setCell(new DateCell(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));
	 	 		
		
	    List<ColumnConfig<SetInfo, ?>> l = new ArrayList<ColumnConfig<SetInfo, ?>>();
	    l.add(expander);
	    l.add(sm.getColumn());
	    l.add(keywordColumn);
	    l.add(authorNameColumn);
	    l.add(tagsColumn);
	    l.add(dateColumn);
	 
	    ColumnModel<SetInfo> cm = new ColumnModel<SetInfo>(l);
	 
	    Grid<SetInfo> grid = new Grid<SetInfo>(store, cm) {
	      @Override
	      protected void onAfterFirstAttach() {
	        super.onAfterFirstAttach();
	        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
	          @Override
	          public void execute() {
	            loader.load();
	          }
	        });
	      }
	    };		
	    grid.getView().setAutoExpandColumn(keywordColumn);
	    grid.setSelectionModel(sm);
	    grid.getView().setForceFit(true);
	    grid.setLoadMask(true);
	    grid.getView().setStripeRows(true);
	    grid.getView().setColumnLines(true);
	    grid.setLoader(loader);
	    
	    expander.initPlugin(grid);
	    
	    FramedPanel cp = new FramedPanel();
	    cp.setCollapsible(true);
	    setBorders(true);
	    add(grid, new VerticalLayoutData(1, 1));
	    add(toolBar, new VerticalLayoutData(1, -1));
	}

}
